#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   3.py
@Time    :   2020/05/25 21:23:05
@Author  :   Malygos_l 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   www.gitee.com/malygos_l
'''
# Start typing your code from here
# 编写一个装饰器，为多个函数加上认证的功能（必须输入用户的账号密码，才能调用这个函数）

import hashlib


def authentication(func):
    username = ''
    password = ''

    def wrapper():
        with open("homework4/nup.txt", "r", encoding='UTF-8') as f:
            self_message = f.readlines()
            users = []
            for message in self_message:
                users.append(message.split())

            username = input("请输入账号：")
            password = input("请输入密码：")
            md5 = hashlib.md5()
            md5.update(password.encode('UTF-8'))
            password = md5.hexdigest()

            #测试数据：
            # malygos_l0
            # 18311010

            for user in users:
                if user[1] == username:
                    if user[2] == password:
                        func()
                        return
                    else:
                        print("账号或密码错误！")
                        return
                else:
                    print("账号或密码错误！")
                    return

    return wrapper


@authentication
def test():
    print("this is A test!")


def main():
    test()


if __name__ == "__main__":
    main()